package com.ruoyi.project.system.requirement.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.post.service.IPostService;
import com.ruoyi.project.system.project.domain.Project;
import com.ruoyi.project.system.project.service.IProjectService;
import com.ruoyi.project.system.record.domain.WorkRecord;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.service.IRoleService;
import com.alibaba.fastjson.JSON;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.system.user.service.UserServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.requirement.domain.Requirement;
import com.ruoyi.project.system.requirement.service.IRequirementService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

import javax.sql.DataSource;


/**
 * 需求列Controller
 * 
 * @author ruoyi
 * @date 2019-11-12
 */
@Controller
@RequestMapping("/system/requirement")
public class RequirementController extends BaseController
{
    private String prefix = "system/requirement";

    @Autowired
    private IRequirementService requirementService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IPostService postService;

    @RequiresPermissions("system:requirement:view")
    @GetMapping()
    public String requirement()
    {
        return prefix + "/requirement";
    }

    /**
     * 查询需求列列表
     */
    @RequiresPermissions("system:requirement:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Requirement requirement)
    {
        startPage();
        // 普通用户只显示分配给自己的需求
        boolean isDevUser = false;
        User user = getSysUser();
        List<Role> roles = user.getRoles();
        for(Role role : roles){
            if("common".equals(role.getRoleKey())){
                isDevUser = true;
                break;
            }
        }
        if(isDevUser){
            requirement.setDevUser(user.getUserId());
        }
        List<Requirement> list = requirementService.selectRequirementList(requirement);
        return getDataTable(list);
    }

    /**
     * 导出需求列列表
     */
    @RequiresPermissions("system:requirement:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Requirement requirement)
    {
        List<Requirement> list = requirementService.selectRequirementList(requirement);
        ExcelUtil<Requirement> util = new ExcelUtil<Requirement>(Requirement.class);
        return util.exportExcel(list, "requirement");
    }

    /**
     * 新增需求列
     */
    @GetMapping("/add")
    public String add(Long projectId, ModelMap mmap)
    {
        Project project = projectService.selectProjectById(projectId);
        mmap.addAttribute("project", project);
        return prefix + "/add";
    }

    /**
     * 新增保存需求列
     */
    @RequiresPermissions("system:requirement:add")
    @Log(title = "需求列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Requirement requirement)
    {
        String id="";
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String temp = sf.format(new Date());
        int num1 = requirementService.selectCount();
        String num2 = String.format("%03d", num1+1);
        String num = temp+num2;
        requirement.setReqSerial(num);

        User user = getSysUser();
        requirement.setInputUser(user.getUserId());
        // 普通用户录入需求默认分配给自己
        boolean isDevUser = false;
        List<Role> roles = user.getRoles();
        for(Role role : roles){
            if("common".equals(role.getRoleKey())){
                isDevUser = true;
                break;
            }
        }
        if(isDevUser){
            requirement.setDevUser(user.getUserId());
            requirement.setReqStatus("02");
        }else{
            requirement.setReqStatus("01");
        }
        requirement.setUpdateTime(new Date());
        return toAjax(requirementService.insertRequirement(requirement));
    }

    /**
     * 修改需求列
     */
    @GetMapping("/edit/{reqId}")
    public String edit(@PathVariable("reqId") Long reqId, ModelMap mmap)
    {
        Requirement requirement = requirementService.selectRequirementById(reqId);
        mmap.put("requirement", requirement);
        return prefix + "/edit";
    }
    @PostMapping("/getReqname")
    @ResponseBody
    public String  getReqname(Long  reqId)
    {
        Requirement requirement = requirementService.selectRequirementById(reqId);
        return JSON.toJSONString(requirement);
    }
    /**
     * 修改保存需求列
     */
    @RequiresPermissions("system:requirement:edit")
    @Log(title = "需求列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Requirement requirement)
    {
        requirement.setUpdateTime(new Date());
        return toAjax(requirementService.updateRequirement(requirement));
    }

    /**
     * 删除需求列
     */
    @RequiresPermissions("system:requirement:remove")
    @Log(title = "需求列", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] reqIds = ids.split(",");
        for(int i=0; i < reqIds.length; i++){
            Long reqId = Long.parseLong(reqIds[i]);
            Requirement requirement = requirementService.selectRequirementById(reqId);
            requirement.setReqStatus("09");
            requirementService.updateRequirement(requirement);
        }
        return toAjax(1);
    }

    @GetMapping( "/assign")
    public String assign(String reqIds, ModelMap mmap)
    {
        mmap.addAttribute("reqIds", reqIds);
        return prefix + "/assign";
    }

    /*
    *保存任务分配
    */
    @PostMapping( "/saveAssign")
    @ResponseBody
    public AjaxResult saveAssign(String reqIds,Long userIds)
    {
        return toAjax(requirementService.saveAssign(reqIds,userIds));
    }

    /*
    * 查看详情
    * */
    @GetMapping("/detail")
    public String detail(Long reqId,ModelMap mmap){
        String workload = this.requirementService.selectWorkRecordBySj(reqId);
        Requirement requirement = this.requirementService.selectRequirementById(reqId);
        mmap.addAttribute("requirement",requirement);
        mmap.addAttribute("workload",workload);
        return prefix + "/detail";
    }

}
